package com.study.system.controller;

import com.study.system.entity.ManageClubLog;
import com.study.system.entity.ManageUserLog;
import com.study.system.vo.ClubInfoVO;
import com.study.system.dto.ResultData;
import com.study.system.entity.Club;
import com.study.system.enums.ResultCode;
import com.study.system.exception.ClubExistException;
import com.study.system.service.ClubService;
import com.study.system.utils.RequestUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/club")
public class ClubController {

    @Resource
    private ClubService clubService;

    /**
     * 查询当前用户参加的所有社团
     *
     * @param request 前端发送的请求
     * @return 查询结果
     */
    @GetMapping("/user_attended_club")
    public ResultData<List<ClubInfoVO>> findAttendedClub(HttpServletRequest request) {
        Integer userId = RequestUtil.getUserId(request);
        List<ClubInfoVO> clubs = clubService.selectAllClubByUserId(userId);
        return ResultData.success(clubs);
    }

    /**
     * 用户加入社团
     *
     * @param request 前端发送的请求
     * @param club    社团信息
     * @return 操作结果
     */
    @PostMapping("/user_insert_club")
    public ResultData<Boolean> insertAddClubLog(HttpServletRequest request, @RequestBody Club club) {
        Integer userId = RequestUtil.getUserId(request);
        Integer clubId = club.getId();
        if (clubService.insertAddClubLog(userId, clubId)) {
            return ResultData.success(true);
        }
        return ResultData.fail(ResultCode.DATABASE_ERROR);
    }

    /**
     * 查询所有可以加入的社团
     *
     * @param request 前端发送的请求
     * @return 查询结果
     */
    @GetMapping("/user_could_attend_club")
    public ResultData<List<ClubInfoVO>> findCouldAttendClub(HttpServletRequest request) {
        Integer userId = RequestUtil.getUserId(request);
        List<ClubInfoVO> clubs = clubService.selectAllClubCouldAttend(userId);
        return ResultData.success(clubs);
    }

    /**
     * 创建社团
     *
     * @param request  前端发送的请求
     * @param clubInfo 社团信息
     * @return 操作结果
     */
    @PostMapping("/create_club")
    public ResultData<Boolean> createClub(HttpServletRequest request, @RequestBody Club clubInfo) {
        Integer userId = RequestUtil.getUserId(request);
        ResultData<Boolean> resultData;
        try {
            Boolean result = clubService.insetClub(userId, clubInfo);
            if (result) {
                resultData = ResultData.success(true);
            } else {
                resultData = ResultData.fail(ResultCode.DATABASE_ERROR);
            }
        } catch (ClubExistException e) {
            resultData = ResultData.fail(ResultCode.CLUB_EXIST);
        }
        return resultData;
    }

    /**
     * 查询所有社团信息
     *
     * @return 查询结果
     */
    @PostMapping("/show_all_club")
    public ResultData<List<ClubInfoVO>> selectAllClub(@RequestBody Map<String, Integer> limitInfo) {
        List<ClubInfoVO> clubs = clubService.selectAllClubLimit(limitInfo.get("pageSize"), limitInfo.get("currentIndex"));
        return ResultData.success(clubs);
    }

    /**
     * 查询所有可以管理的社团信息
     *
     * @param request 前端发送的请求（只需要Token）
     * @return 查询结果
     */
    @GetMapping("/show_all_manage_club")
    public ResultData<List<Club>> selectAllManageClub(HttpServletRequest request) {
        Integer userId = RequestUtil.getUserId(request);
        List<Club> clubs = clubService.selectAllManageClub(userId);
        return ResultData.success(clubs);
    }

    /**
     * 用户退出社团
     *
     * @param request  请求头（只需要token）
     * @param clubInfo 社团信息（只需要社团ID）
     * @return 操作是否成功
     */
    @PostMapping("/user_exit_club")
    public ResultData<Boolean> userExitClub(HttpServletRequest request, @RequestBody Map<String, Integer> clubInfo) {
        Integer userId = RequestUtil.getUserId(request);
        Integer clubId = clubInfo.get("clubId");
        return ResultData.success(clubService.userExitClub(userId, clubId));
    }

    /**
     * 修改社团信息
     *
     * @param clubInfo 新的社团信息
     * @return 操作是否成功
     */
    @PostMapping("/updateClubInfo")
    public ResultData<Boolean> updateCLubInfo(@RequestBody Club clubInfo) {
        return ResultData.success(clubService.updateClubInfo(clubInfo));
    }

    @PostMapping("/adminUpdateClubInfo")
    public ResultData<Boolean> adminUpdateClubInfo(@RequestBody Club clubInfo) {
        return ResultData.success(clubService.adminUpdateClubInfo(clubInfo));
    }

    /**
     * 获取指定社团信息
     *
     * @param clubInfoVO 社团信息
     * @return 获取的社团信息
     */
    @PostMapping("/getClubInfo")
    public ResultData<ClubInfoVO> getClubInfoById(@RequestBody ClubInfoVO clubInfoVO) {

        return ResultData.success(clubService.getClubInfoById(clubInfoVO));
    }

    /**
     * 社团踢出成员
     *
     * @param request 前端发送的请求
     * @param clubInfo 用户ID 和 社团ID
     * @return 操作是否成功
     */
    @PostMapping("/deleteMember")
    public ResultData<Boolean> deleteMember(HttpServletRequest request, @RequestBody Map<String, Integer> clubInfo) {
        Integer userId = RequestUtil.getUserId(request);
        return ResultData.success(clubService.deleteMember(userId, clubInfo.get("clubId"), clubInfo.get("userId")));
    }

    @PostMapping("/getMemberNumber")
    public ResultData<Integer> getMemberNumber(@RequestBody Map<String, Integer> clubInfo) {
        return ResultData.success(clubService.getMemberNumber(clubInfo.get("id")));
    }

    @GetMapping("/getManageClubLog")
    public ResultData<List<ManageClubLog>> getAllManageClubLog() {
        return ResultData.success(clubService.getAllManageClubLog());
    }

    @GetMapping("/getManageUserLog")
    public ResultData<List<ManageUserLog>> getAllManageUserLog() {
        return ResultData.success(clubService.getAllManageUserLog());
    }

    @GetMapping("/getClubCount")
    public ResultData<Integer> getClubCount() {
        return ResultData.success(clubService.getClubCount());
    }
}
